import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

public class AESEncryptor {
    private static final String AES_ALGORITHM = "AES";
    private static final String AES_KEY = "0123456789abcdef"; // 128-bit key

    public static String AES128Encrypt(String plaintext, String key) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), AES_ALGORITHM);
        Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static String AES128Decrypt(String ciphertext, String key) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), AES_ALGORITHM);
        Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(ciphertext));
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }

    public static void encryptImage(String inputImagePath, String outputImagePath) throws Exception {
        String originS=ImageString.getImageBinary(inputImagePath);
        String key = "0123456789abcdef"; // 128-bit key
        String aesS=AES128Encrypt(originS,key);
        ImageString.base64StringToImage(aesS,outputImagePath);
    }

    public static void generateSnowflake(String inputImagePath, String outputImagePath) throws Exception {
        BufferedImage image = ImageIO.read(new File(inputImagePath));
        StringBuilder sb = new StringBuilder();
        int imageHeight = image.getHeight();
        int imageWidth = image.getWidth();
        for (int y = 0; y < imageHeight; y++) {
            for (int x = 0; x < imageWidth; x++) {
                int rgba = image.getRGB(x, y);
                sb.append(rgba);
            }
        }
        String pixelData = sb.toString();
        String encryptedData = AES128Encrypt(pixelData, AES_KEY);
        byte[] encryptedBytes = encryptedData.getBytes();
        BufferedImage encryptedImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
        int byteIndex = 0;
        for (int y = 0; y < imageHeight; y++) {
            for (int x = 0; x < imageWidth; x++) {
                if (byteIndex < encryptedBytes.length) {
                    int pixelValue = encryptedBytes[byteIndex++] & 0xFF;
                    encryptedImage.setRGB(x, y, (pixelValue << 16) | (pixelValue << 8) | pixelValue);
                } else {
                    encryptedImage.setRGB(x, y, 0); // 填充剩余像素为黑色
                }
            }
        }
        ImageIO.write(encryptedImage, "png", new File(outputImagePath));
    }

//    public static String AES128Encrypt(String data, String key) throws Exception {
//        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
//        Cipher cipher = Cipher.getInstance("AES");
//        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
//        byte[] encryptedData = cipher.doFinal(data.getBytes());
//        return Base64.getEncoder().encodeToString(encryptedData);
//    }
//
//    public static String AES128Decrypt(String encryptedData, String key) throws Exception {
//        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
//        Cipher cipher = Cipher.getInstance("AES");
//        cipher.init(Cipher.DECRYPT_MODE, secretKey);
//        //填充并替换非法字符
//        byte[] originalData = new byte[0];
//        encryptedData = encryptedData.replaceAll("[^A-Za-z0-9+/]", "+");
//        while (encryptedData.length() % 64 != 0) {
//            encryptedData += "+";
//        }
////        try {
////            byte[] decodedData= Base64.getDecoder().decode(encryptedData);
////            originalData = cipher.doFinal(decodedData);
////        } catch (IllegalArgumentException e) {
////            System.out.println("Invalid Base64 input: " + e.getMessage());
////        }
//        System.out.println(encryptedData.length());
//        byte[] decodedData= Base64.getDecoder().decode(encryptedData);
////        byte[] decodedData = Base64.getUrlDecoder().decode(encryptedData);
////        byte[] decodedData=base64.decodeBase64(encryptedData.getBytes());
////        while (decodedData.length % 16 != 0) {
////            List<byte> list = new ArrayList<byte>();
////            decodedData = ArrayUtils.addAll(pri, frag);
////            decodedData += '0';
////        }
//        System.out.println(decodedData.length);
//
//        originalData = cipher.doFinal(decodedData);
//        return new String(originalData);
//    }


}

